﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using HCI.Questionaire.Entities;

namespace HCI.Questionaire.QuestionRender
{
    public partial class QuestionGroup3Render : UserControl, IQuestionGroupRenderControl
    {
        #region Feild
        private StackPanel _currentFieldItem;
        private TextBox txtSecoundAnswer;
        #endregion

        #region Constructor
        public QuestionGroup3Render()
        {
            InitializeComponent();
        }

        #endregion

        #region IQuestionGroupRenderControl Members

        public void InitailRender(HCI.Questionaire.QuestionGroup.IQuestionGroup questionGroup)
        {

            this.LayoutRoot.RowDefinitions[0].Height = questionGroup.CurrentQuestionIndex == 0 ? new GridLength(48) : new GridLength(80);
            this.DataContext = new RatingQuestionInfo
            {
                QuestionText = ((RatingQuestionGroup)questionGroup.QuestionList[questionGroup.CurrentQuestionIndex]).QuestionText,
                FirstPageVisible = questionGroup.CurrentQuestionIndex == 0 ? Visibility.Visible : Visibility.Collapsed,
                SecoundPageVisible = questionGroup.CurrentQuestionIndex == 1 ? Visibility.Visible : Visibility.Collapsed,
                Description = "What WHAT wHaT??",
                QuestionCollection = ((RatingQuestionGroup)questionGroup.QuestionList[questionGroup.CurrentQuestionIndex]).QuestionList
            };
        }

        public string QuestionRunningNumber
        {
            get
            {
                return this.txtQuestionRunningNumber.Text;
            }
            set
            {
                this.txtQuestionRunningNumber.Text = value;
            }
        }
        #endregion

        // TODO: Refactor this event to centralize
        #region Eventhandler: Item
        private void Item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this._currentFieldItem != null)
            {
                ((Border)this._currentFieldItem.Children[0]).BorderBrush = new SolidColorBrush(Color.FromArgb(255, 204, 204, 204));
                ((Border)((StackPanel)sender).Children[0]).BorderThickness = new Thickness(1.0);
            }
            ((Border)((StackPanel)sender).Children[0]).BorderBrush = this.fieldsBorderBrush;
            ((Border)((StackPanel)sender).Children[0]).BorderThickness = new Thickness(1);
            this._currentFieldItem = (StackPanel)sender;
        }

        private void Item_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Border)sender).BorderBrush = this.fieldsBorderBrush;
            ((Border)sender).BorderThickness = new Thickness(1);

            this.fieldMouseOver.Stop();
            Storyboard.SetTarget(this.fieldMouseOver, (DependencyObject)sender);
            this.fieldMouseOver.Begin();
        }

        private void Item_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this._currentFieldItem != null && ((Border)((StackPanel)this._currentFieldItem).Children[0]) == ((Border)sender))
            {
                return;
            }

            ((Border)sender).BorderBrush = new SolidColorBrush(Colors.Gray);
            ((Border)sender).BorderThickness = new Thickness(1);
            this.fieldMouseOut.Stop();
            Storyboard.SetTarget(this.fieldMouseOut, (DependencyObject)sender);
            this.fieldMouseOut.Begin();

        }
        #endregion

        #region Eventhandler: TextBox Input
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void TextBox_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtSecoundAnswer = sender as TextBox;
            this.Dispatcher.BeginInvoke(delegate { this.txtSecoundAnswer.Focus(); });
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            var ques = ((TextBox)sender).DataContext as FillTextQuestion;
            if (ques != null)
            {
                ques.AnswerText = ((TextBox)sender).Text;
            }
        }
        #endregion

        #region Inner class: RatingQuestionInfo
        public class RatingQuestionInfo
        {
            public object AnswerText { get; set; }
            public string QuestionText { get; set; }
            public string Description { get; set; }
            public List<RatingQuestion> QuestionCollection { get; set; }
            public Visibility FirstPageVisible { get; set; }
            public Visibility SecoundPageVisible { get; set; }
        }
        #endregion

    }
}
